package eu.news.template;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import eu.news.config.SystemConfig;
import eu.news.data.hibernate.pojo.HB_News;
import eu.news.data.pojo.News;

public class WebPageHelper {
	
	public static String news(HB_News news,HttpServletRequest request) throws IOException{
		String templatePath=news.getSort().getSpecial().getTheme();
		if(templatePath==null)
			templatePath=SystemConfig.getDefaultTheme();
		String tpName="news";
		if(news.getType()==News.Type.PICTURE)
			tpName="gallery";
		String template = TPLUtils.read(templatePath, tpName);
		WebPage page=new WebPage(template);
		page.setParameter("news", news);
		page.setParameter("base", getBaseURL(request));
		return page.generateHtml();
	}
	
	public static String searchResult(List<HB_News> newses,String keyword,HttpServletRequest request) throws IOException{
		String template = TPLUtils.read(SystemConfig.getDefaultTheme(), "searchResult");
		WebPage page=new WebPage(template);
		page.setParameter("newses", newses);
		page.setParameter("base", getBaseURL(request));
		page.setParameter("keyword", keyword);
		return page.generateHtml();
	}
	
	private static String getBaseURL(HttpServletRequest request){
		return request.getScheme() + "://"
				+ request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath() + "/";
	}
}
